package com.gjj.abox.entity;

import com.gjj.abox.constant.FileTypes;

import java.util.Comparator;
import java.util.Locale;

/**
 * Created by ${GUJIAJIA} on 2016/4/5.
 * E-mail:why.name@outlook.com
 * Phone :15050261230
 */
public class FileInfo {

    enum FileType {
        FILE , DIRECTORY;
    }

    private FileType fileType;
    private String fileName;
    private String filePath;
    private double fileSize;

    public FileInfo(String filePath, String fileName,double fileSize, boolean isDirectory) {
        this.filePath = filePath;
        this.fileName = fileName;
        this.fileSize = fileSize;
        fileType = isDirectory ? FileType.DIRECTORY : FileType.FILE;
    }

    public boolean isDBFile(){
        if(fileName.lastIndexOf(".") < 0)  //Don't have the suffix
            return false ;
        if(!isDirectory() && FileTypes.DB_SUFFIX.contains(getFileType(fileName)))
            return true ;
        else
            return false ;
    }

    public boolean isPPTFile(){
        if(fileName.lastIndexOf(".") < 0)  //Don't have the suffix
            return false ;
        if(!isDirectory() && FileTypes.PPT_SUFFIX.contains(getFileType(fileName)))
            return true ;
        else
            return false ;
    }
    public boolean isVoiceFile(){
        if(fileName.lastIndexOf(".") < 0)  //Don't have the suffix
            return false ;
        if(!isDirectory() && FileTypes.VOICE_SUFFIX.contains(getFileType(fileName)))
            return true ;
        else
            return false ;
    }

    public boolean isTxtFile(){
        if(fileName.lastIndexOf(".") < 0)  //Don't have the suffix
            return false ;
        if(!isDirectory() && FileTypes.TXT_SUFFIX.contains(getFileType(fileName)))
            return true ;
        else
            return false ;
    }

    public boolean isZipFile(){
        if(fileName.lastIndexOf(".") < 0)  //Don't have the suffix
            return false ;
        if(!isDirectory() && FileTypes.ZIP_SUFFIX.contains(getFileType(fileName)))
            return true ;
        else
            return false ;
    }

    public boolean isExcelFile(){
        if(fileName.lastIndexOf(".") < 0)  //Don't have the suffix
            return false ;
        if(!isDirectory() && FileTypes.EXCEL_SUFFIX.contains(getFileType(fileName)))
            return true ;
        else
            return false ;
    }

    public boolean isPictureFile(){
        if(fileName.lastIndexOf(".") < 0)  //Don't have the suffix
            return false ;
        if(!isDirectory() && FileTypes.PICTURE_SUFFIX.contains(getFileType(fileName)))
            return true ;
        else
            return false ;
    }

    public boolean isMovieFile(){
        if(fileName.lastIndexOf(".") < 0)  //Don't have the suffix
            return false ;
        if(!isDirectory() && FileTypes.MOV_SUFFIX.contains(getFileType(fileName)))
            return true ;
        else
            return false ;
    }

    public boolean isApkFile(){
        if(fileName.lastIndexOf(".") < 0)  //Don't have the suffix
            return false ;
        if(!isDirectory() && FileTypes.APK_SUFFIX.contains(getFileType(fileName)))
            return true ;
        else
            return false ;
    }

    public boolean isWordFile(){
        if(fileName.lastIndexOf(".") < 0)  //Don't have the suffix
            return false ;
        if(!isDirectory() && FileTypes.WORD_SUFFIX.contains(getFileType(fileName)))
            return true ;
        else
            return false ;
    }

    public boolean isDirectory(){
        if(fileType == FileType.DIRECTORY)
            return true ;
        else
            return false ;
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public String getFilePath() {
        return filePath;
    }

    public void setFilePath(String filePath) {
        this.filePath = filePath;
    }

    @Override
    public String toString() {
        return "FileInfo [fileType=" + fileType + ", fileName=" + fileName
                + ", filePath=" + filePath + "]";
    }
    public String getFileType(String string){
        String fileSuffix="";
        if(string.lastIndexOf(".")==-1){
            fileSuffix = "errorType";
        }else{
            fileSuffix = (string.substring(string.lastIndexOf("."))).toUpperCase(Locale.getDefault());
        }
        return fileSuffix;
    }

    public double getFileSize() {
        return fileSize;
    }

    public void setFileSize(double fileSize) {
        this.fileSize = fileSize;
    }

    @SuppressWarnings("rawtypes")
    public static class ComparatorUser implements Comparator {

        public int compare(Object o1, Object o2) {
            FileInfo file1 = (FileInfo) o1;
            FileInfo file2 = (FileInfo) o2;
            return  (file1.getFileName().toUpperCase(Locale.getDefault())).compareTo(file2.getFileName().toUpperCase(Locale.getDefault()));
        }
    }

}
